<template>
  <el-dialog
    v-el-drag-dialog
    title="备注"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :model="temp" :rules="rules" label-position="right" label-width="100px">
      <el-form-item label="备注:" prop="remark">
        <el-input v-model="temp.remark" disabled />
      </el-form-item>
      <el-form-item label="处理备注:" prop="addRemark">
        <el-input v-model.trim="temp.addRemark" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryOtherOrderRemark } from '@/api'

  export default {
    name: 'remark',
    data() {
      return {
        visible: false,
        temp: {
          remark: '',
          addRemark: ''
        },
        rules: {
          addRemark: [{ required: true, message: '请输入要追加的备注', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) this.temp = Object.assign(this.temp, temp)
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          let remark = this.temp.remark
          if (this.temp.addRemark) {
            if (this.temp.remark) {
              remark += (',' + this.temp.addRemark)
            } else {
              remark = this.temp.addRemark
            }
          }
          if (remark.length > 255) {
            this.$message.error('备注长度太长!')
            return
          }
          const params = { otherCode: this.temp.otherCode, addRemark: remark }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryOtherOrderRemark(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '追加备注成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
